/**
 * 
 */
package com.moioo.exam.dwr;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import com.moioo.util.User;

/**
 * @author 3400423@qq.com
 *
 */
public class ExamManager {
	public static class ExamUser{
		private Integer examid;
		private long hits;
		private User user;
		private Timestamp startTime = null;
		public ExamUser(Integer id,User user){
			examid = id;
			this.user = user;
			hits = System.currentTimeMillis();
			startTime = new Timestamp(hits);
		}
		public User getUser() {
			return user;
		}
		public void setUser(User user) {
			this.user = user;
		}
		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + ((user == null) ? 0 : user.hashCode());
			return result;
		}
		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			ExamUser other = (ExamUser) obj;
			if (user == null) {
				if (other.user != null)
					return false;
			} else if (!user.equals(other.user))
				return false;
			return true;
		}
		public Timestamp getStartTime() {
			return startTime;
		}
		public void setStartTime(Timestamp startTime) {
			this.startTime = startTime;
		}
		
	}
	public static class ObjectExistException extends Exception{
		private static final long serialVersionUID = 874212158306805161L;	
	}
	public class ExamTask extends TimerTask{
		private ExamManager mutex = null;
		public ExamTask(ExamManager o){
			mutex = o;
		}
		@Override
		public void run() {
			System.out.println("-----------------------exam thread run---------------------------------");
			mutex.Clear();
		}	
	}
	public ExamManager(){
		timer = new Timer();
		ExamTask task = new ExamTask(this);
		timer.scheduleAtFixedRate(task, new Date(System.currentTimeMillis()), 1000*60*2);
	}
	private Timer timer = null;
	private java.util.Set<ExamUser> users = Collections.synchronizedSet(new java.util.LinkedHashSet<ExamUser>());
	private final static long dep = 1000*60*3;
	public synchronized void Clear(){
		Iterator<ExamUser> it = users.iterator();
		while(it.hasNext()){
			ExamUser o = it.next();
			System.out.printf("examing:%s", o.getUser().getName());
			if((System.currentTimeMillis()) - o.hits > dep){
				System.out.printf("exam success:%s", o.getUser().getName());
				it.remove();
			}
		}
	}
	public synchronized void hits(User user){
		for(ExamUser u : users){
			if(u.user.getId() == user.getId()){
				u.hits = System.currentTimeMillis();
			}
		}
	}
	public synchronized void delete(User user){
		Iterator<ExamUser> it = users.iterator();
		while(it.hasNext()){
			ExamUser o = it.next();
			if(o.user.getId() == user.getId()){it.remove();break;}
		}
	}
	public synchronized void put(User user,Integer examid)throws Exception{
		ExamUser u = new ExamUser(examid,user);
		if(users.contains(u))throw new ObjectExistException();
		users.add(u);
	}
	public synchronized List<ExamUser> list(Integer id){
		List<ExamUser> rtv = new ArrayList<ExamUser>();
		for(ExamUser u : users){
			if(u.examid.intValue() == id.intValue())
				rtv.add(u);
		}
		return rtv;
	}
}
